package com.hq.fxh.No3_TuiGuang.Adaoter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hq.fxh.Modle.GetPushDetailInfoRes;

import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.ScreenManager;
import com.hq.fxh.Utils.ScreenUtils;
import com.hq.fxh.Utils.SystemUtils;

import org.apmem.tools.layouts.FlowLayout;

import java.util.List;




/**
 * @author 小谢
 * @ClassName PushDetailInfoActivityAdapter
 * @Description :
 * @Created 2017/5/19 0019
 */

public class PushDetailInfoActivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //item类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    public static final int ITEM_TYPE_BOTTOM = 2;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private GetPushDetailInfoRes.DataBean dates;
    private int mHeaderCount = 1;//头部View个数
    private int mBottomCount = 1;//底部View个数
    private boolean isopen = false;

    public PushDetailInfoActivityAdapter(Context context, GetPushDetailInfoRes.DataBean dates) {
        mContext = context;
        this.dates = dates;
        mLayoutInflater = LayoutInflater.from(context);
    }


    //内容长度
    public int getContentItemCount() {
//        return dates.getEnterprises() != null ? dates.getEnterprises().size() : 0;
        if (dates.getEnterprises() == null) {
            return 0;
        } else if (dates.getEnterprises().size() <= 3) {
            return dates.getEnterprises().size();
        } else if (dates.getEnterprises().size() > 3 && isopen) {
            return dates.getEnterprises().size();
        } else if (dates.getEnterprises().size() > 3 && !isopen) {
            return 3;
        }
        return 0;
    }

    //判断当前item是否是HeadView
    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    //判断当前item是否是FooterView
    public boolean isBottomView(int position) {
        return mBottomCount != 0 && position >= (mHeaderCount + getContentItemCount());
    }

    //判断当前item类型
    @Override
    public int getItemViewType(int position) {
        int dataItemCount = getContentItemCount();
        if (mHeaderCount != 0 && position < mHeaderCount) {
//头部View
            return ITEM_TYPE_HEADER;
        } else if (mBottomCount != 0 && position >= (mHeaderCount + dataItemCount)) {
//底部View
            return ITEM_TYPE_BOTTOM;
        } else {
//内容View
            return ITEM_TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) {
            return new HeaderViewHolder(mLayoutInflater.inflate(R.layout.layout_inter_push_business_head, parent, false));
        } else if (viewType == ITEM_TYPE_CONTENT) {
            return new ContentViewHolder(mLayoutInflater.inflate(R.layout.layout_inter_push_business_content_item, parent, false));
        } else if (viewType == ITEM_TYPE_BOTTOM) {
            int width = SystemUtils.getWindowWidth((Activity) parent.getContext());
//            return new BottomViewHolder(mLayoutInflater.inflate(R.layout.layout_inter_push_business_foot, parent, false));
            BottomViewHolder holder = new BottomViewHolder(mLayoutInflater.inflate(R.layout.layout_inter_push_business_foot, parent, false));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,(width/4));
            holder.ad_img2.setLayoutParams(params);
            return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //头部
        if (holder instanceof HeaderViewHolder) {
        }
        //内容
        else if (holder instanceof ContentViewHolder) {
            ((ContentViewHolder) holder).content_companyName.setText(dates.getEnterprises().get(position - mHeaderCount));
        }
        //尾部
        else if (holder instanceof BottomViewHolder) {
            SetBusDate((BottomViewHolder) holder, dates.getEnterprises() != null ? dates.getEnterprises().size() : 0);
            BusinessDateListener((BottomViewHolder) holder);
            SetIndustryDate((BottomViewHolder) holder, dates.getTrades());
            SetCityDate((BottomViewHolder) holder, dates.getCitys());
            SetAdDate((BottomViewHolder) holder, dates.getTemplate());
        }
    }

    private void SetBusDate(final BottomViewHolder holder, int contentItemCount) {
        if (contentItemCount == 0) {
            holder.business_isDate.setVisibility(View.VISIBLE);
            holder.business_isMore.setVisibility(View.GONE);
        } else if (contentItemCount <= 3) {
            holder.business_isDate.setVisibility(View.GONE);
            holder.business_isMore.setVisibility(View.GONE);
        } else {
            holder.business_isDate.setVisibility(View.GONE);
            holder.business_isMore.setVisibility(View.VISIBLE);
        }
    }

    private void SetCityDate(BottomViewHolder holder, List<String> data) {
        holder.area_Data.removeAllViews();
        if (data != null && data.size() > 0) {
            holder.area_Data.setVisibility(View.VISIBLE);
            holder.area_isDate.setVisibility(View.GONE);
            for (int i = 0; i < data.size(); i++) {
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                View view = (View) layoutInflater.inflate(R.layout.item_push_info, null);
                view.setTag(data.get(i));
                TextView tv = (TextView) view.findViewById(R.id.push_item);
                tv.setText(data.get(i));
                holder.area_Data.addView(view);
            }
        } else {
            holder.area_Data.setVisibility(View.GONE);
            holder.area_isDate.setVisibility(View.VISIBLE);
        }
    }

    private void SetIndustryDate(BottomViewHolder holder, List<String> data) {
        holder.industry_Data.removeAllViews();
        if (data != null && data.size() > 0) {
            holder.industry_Data.setVisibility(View.VISIBLE);
            holder.industry_isDate.setVisibility(View.GONE);
            for (int i = 0; i < data.size(); i++) {
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                View view = (View) layoutInflater.inflate(R.layout.item_push_info, null);
                view.setTag(data.get(i));
                TextView tv = (TextView) view.findViewById(R.id.push_item);
                tv.setText(data.get(i));
                holder.industry_Data.addView(view);
            }
        } else {
            holder.industry_Data.setVisibility(View.GONE);
            holder.industry_isDate.setVisibility(View.VISIBLE);
        }
    }

    private void BusinessDateListener(final BottomViewHolder holder) {

        holder.business_isMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isopen = !isopen;
                if (isopen) {
                    holder.business_isMore.setText("收起");
                    changeImage(holder.business_isMore, R.mipmap.xx_shang);//显示全部数据
                    notifyDataSetChanged();
                } else {
                    holder.business_isMore.setText("显示全部");
                    changeImage(holder.business_isMore, R.mipmap.xx_xia);//只显示三条数据
                    notifyDataSetChanged();
                }
            }
        });
    }

    private void changeImage( TextView tv, int imgId) {
        Drawable nav_up = mContext.getResources().getDrawable(imgId);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        tv.setCompoundDrawables(null, null, nav_up, null);
    }

    private void SetAdDate(BottomViewHolder holder, GetPushDetailInfoRes.DataBean.TemplateBean data) {
        if (data != null) {
            holder.ad_isDate.setVisibility(View.GONE);
            holder.ad_rl.setVisibility(View.VISIBLE);
            if ("信息流广告".equals(data.getType_name())) {
                holder.ad_rl.setVisibility(View.GONE);
                holder.ad_ll_xinxiliu.setVisibility(View.VISIBLE);
                 GlideUtils.loadImage(mContext,data.getPic(),holder.ad_img2);
                holder.ad_title2.setText(data.getTitle());
            } else {
                holder.ad_rl.setVisibility(View.VISIBLE);
                holder.ad_ll_xinxiliu.setVisibility(View.GONE);
                GlideUtils.loadImage(mContext,data.getPic(),holder.ad_img);

                 holder.ad_title.setText(data.getTitle());
                holder.ad_type.setText(data.getType_name());
            }
        } else {
            holder.ad_isDate.setVisibility(View.VISIBLE);
            holder.ad_rl.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mHeaderCount + getContentItemCount() + mBottomCount;
    }

    //内容 ViewHolder
    public static class ContentViewHolder extends RecyclerView.ViewHolder {
        private TextView content_companyName;

        public ContentViewHolder(View itemView) {
            super(itemView);
            content_companyName = (TextView) itemView.findViewById(R.id.content_companyName);
        }
    }

    //头部 ViewHolder
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    //底部 ViewHolder
    public static class BottomViewHolder extends RecyclerView.ViewHolder {
        private TextView business_isDate;
        private TextView business_isMore;
        private TextView industry_isDate;
        private FlowLayout industry_Data;
        private TextView area_isDate;
        private FlowLayout area_Data;
        private ImageView ad_img;
        private TextView ad_title;
        private TextView ad_type;
        private TextView ad_isDate;
        private RelativeLayout ad_rl;
        private LinearLayout ad_ll_xinxiliu;
        private final TextView ad_title2;
        private final ImageView ad_img2;

        public BottomViewHolder(View itemView) {
            super(itemView);
            ad_ll_xinxiliu=(LinearLayout)itemView.findViewById(R.id.ad_ll_xinxiliu);
            ad_title2 =(TextView)itemView.findViewById(R.id.ad_title2);
            ad_img2 =(ImageView)itemView.findViewById(R.id.ad_img2);
            business_isDate = (TextView) itemView.findViewById(R.id.business_isDate);
            business_isMore = (TextView) itemView.findViewById(R.id.business_isMore);
            industry_isDate = (TextView) itemView.findViewById(R.id.industry_isDate);
            industry_Data = (FlowLayout) itemView.findViewById(R.id.industry_Data);
            area_isDate = (TextView) itemView.findViewById(R.id.area_isDate);
            area_Data = (FlowLayout) itemView.findViewById(R.id.area_Data);
            ad_img = (ImageView) itemView.findViewById(R.id.ad_img);
            ad_title = (TextView) itemView.findViewById(R.id.ad_title);
            ad_type = (TextView) itemView.findViewById(R.id.ad_type);
            ad_isDate = (TextView) itemView.findViewById(R.id.ad_isDate);
            ad_rl = (RelativeLayout) itemView.findViewById(R.id.ad_rl);
        }
    }
}
